package vn.sunnet.backviet01.game.coloa.human.enemy;

import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import vn.sunnet.backviet01.game.coloa.constant.Constants;

public class Tank extends Enemy{
	private static long durationMove = 100;
	private static long durationAttack = 120;
	private static long durationDie = 150;
	
	private long countDelay = 0;
		
	public Tank(int tiledAttack, float pX, float pY,
			TiledTextureRegion pTiledTextureRegion) {
		super(tiledAttack, pX, pY, pTiledTextureRegion);
		this.name = Constants.TANK;
	}
	
	public void onManagedUpdate(final float pSecondsElapsed) {
		if (isAllowChangeStatus) {
			stopAnimation();
			if (this.status == Constants.DIE) {
				updateTextureRegion(mTextureRegion_next.deepCopy(), true);
				animate(durationDie);
			} else if (isFreezed && countDelay == 0) {
				updateTextureRegion(mTextureRegion_next.deepCopy(), true);
				countDelay = System.currentTimeMillis();
				this.handler.setEnabled(false);
				this.setPosition(this.getX(), this.getY() + 10);
			} else if (isStun && startStun == 0) {
				setStunAction();
			} else if (this.status == Constants.ATTACK) {
				updateTextureRegion(mTextureRegion_next.deepCopy(), true);
				this.setPosition(this.getX(), this.getY() - 10);
				animate(durationAttack);
			}
			isAllowChangeStatus = false;
		}  else if (isFreezed && this.hitPoints > 0) {
			if (countDelay + timeFreezed < System.currentTimeMillis()) {
				resetMove();
				isFreezed = false;
				countDelay = 0;
			}
		} else if (isStun && this.hitPoints > 0) {
			if (timeStun + startStun < System.currentTimeMillis()) {
				resetStunAction();
				isStun = false;
				startStun = 0;
			}
		}
		
		if (status == Constants.ATTACK
				&& this.getCurrentTileIndex() != tiledAttack)
			isAttack = false;

		else if (status == Constants.DIE && this.life) {
			setAlpha(getAlpha() - 0.02f);
		}
		
		super.onManagedUpdate(pSecondsElapsed);
		
		this.xContain = this.getX() + 10;
		this.yContain = this.getY() + 15;
	}

//	public void setAttackTextureRegion(TiledTextureRegion pAttackTextureRegion) {
//		mAttackTextureRegion = pAttackTextureRegion;
//	}
	
	public void updateTextureRegion (TiledTextureRegion pTextureRegion, boolean isReset) {
		super.updateTextureRegion(pTextureRegion, isReset);
		widthContain = pTextureRegion.getTileWidth() - 20;
		heightContain = pTextureRegion.getTileHeight() - 20;
	}
	
	protected void createContain() {
		xContain = this.getX() + 10;
		yContain = this.getY() + 10;

		widthContain = this.getWidth() - 20;
		heightContain = this.getHeight() - 20;
	}

	public void resetMove() {
		updateTextureRegion(mTextureRegionMove.deepCopy(), true);
		this.setPosition(this.getX(), this.getY());
		this.animate(durationMove);
		handler.setEnabled(true);
		status = Constants.MOVE;
	}


	public void setStunAction() {
		if (isFreezed) {
			isStun = false;
			return;
		}

		startStun = System.currentTimeMillis();
		
		switch (status) {
		case Constants.MOVE:
			int index = this.getCurrentTileIndex();
			if (getTextureRegion() != mTextureRegionMove)
				updateTextureRegion(mTextureRegionMove, true);
			animate((long)(durationMove * 2));
			handler.setVelocityX(handler.getVelocityX() / 2);
			setCurrentTileIndex(index);
			
			break;
		case Constants.ATTACK:
			int index1 = this.getCurrentTileIndex();
			animate((long)(durationAttack * 2));
			setCurrentTileIndex(index1);
			break;
		default:
			break;
		}
	}
	
	public void resetStunAction() {
		switch (status) {
		case Constants.MOVE:
			int index = this.getCurrentTileIndex();
			animate(durationMove);
			handler.setVelocityX(handler.getVelocityX() * 2);
			setCurrentTileIndex(index);
			
			break;
		case Constants.ATTACK:
			int index1 = this.getCurrentTileIndex();
			animate(durationAttack);
			setCurrentTileIndex(index1);
			break;
		default:
			break;
		}
	}
	
}
